// global-components/lk-bind-phone/lk-bind-phone.js
// components/bind-phone/index.js
// this.triggerEvent('success',{ "mobile": this.data.form.phone }) 这个是绑定成功的回调
import api from "../../utils/api"
import CONFIG from '../../utils/config'

const app = getApp()

Component({
  properties: {
    msgContent: {
      type: String,
    },
    msgTitle:{
      type: String,
      value: '请绑定手机号',
    },
    phoneType:{
      type:Number,
      value: 1,// 1 显示绑定手机跟微信手机号  2 只有绑定手机号  3 只有微信绑定手机号 4  只有绑定手机号跟添加姓名
    },
    //微信按钮
    wxButtonColor:{
      type:String,
      value:"#fe9b02"
    },
    //按钮文本颜色
    wxButtonTextColor:{
      type: String,
      value: "#fff"
    },
    //绑定手机按钮颜色
    buttonColor:{
      type: String,
      value: "#F3F3F3"
    },
    //绑定手机按钮颜色
    buttonColorPressed: {
      type: String,
      value: "#3399EA"
    },
    //按钮文本颜色
    buttonTextColor:{
      type: String,
      value: "#fff"
    },
    //是否显 销毁的icon
    isShowClose:{
      type: Boolean,
      value:true
    },
    //白色距离顶部的距离
    marginTop:{
      type:String,
      value: "300rpx"
    }
  },
  options: {
    addGlobalClass: true
  },
  data: {
    btn: {
      text: '获取验证码',
      time: 60,
      timeFn: null,
      disabled: false
    },
    form: {
      phone: '',
      code: '',
      name:'',
    },
    isshow:false,
    isPresss:false,
  },
  methods: {
    /**
     * 显示弹窗
     */
    show() {
      this.setData({
        isshow: true
      })
    },
    /**
     * 取消
     */
    closeDialog(){
      this.setData({
        isshow: false
      })
    },
    // 获取验证码
    getYzmFn() {
      let btn = this.data.btn
      const sTime = btn.time
      btn.time -= 1
      this.setData({
        'btn.disabled': true,
        'btn.time': btn.time,
        'btn.text': `${btn.time}S后重发`
      })
      btn.text = `${btn.time}S后重发`
      btn.timeFn = setInterval(() => {
        btn.time -= 1
        this.setData({
          'btn.disabled': true,
          'btn.time': btn.time,
        })
        if (btn.time <= 0) {
          clearInterval(btn.timeFn)
          this.setData({
            'btn.disabled': false,
            'btn.text': '发送验证码',
            'btn.time': sTime
          })
          return false
        }
        this.setData({
          'btn.text': `${btn.time}S后重发`
        })
      }, 1000)
    },
    // 输入框双向绑定
    handleModelValue(e) {
      let value = e.detail.value
      let model = e.target.dataset.model
      this.setData({
        [model]: value
      })
      if (model === 'form.code'){
        var length = value.length
        if(length == 0){
          if(this.data.isPresss){
            this.setData({
              isPresss:false
            })
          }
        }else{
          if (!this.data.isPresss) {
            this.setData({
              isPresss: true
            })
          }
        }
      }
    },
    // 提交绑定
    submit() {
      let form = this.data.form
      if (form.phone.length < 11) {
        wx.showToast({
          icon: 'none',
          title: '请输入正确的手机号码',
        })
        return
      }
      if (form.code.length === 0) {
        wx.showToast({
          icon: 'none',
          title: '请输入验证码',
        })
        return
      }
      if (form.name.length === 0 && this.data.phoneType == 4) {
        wx.showToast({
          icon: 'none',
          title: '请输入姓名',
        })
        return
      }
      let data = {
        code: form.code,
        mobile: form.phone
      }
      if (this.data.phoneType == 4){
        data.username = form.name
      }
      api.post('/api/user/saveusermobile', {
        data: data
      }).then(res => {
        app.mdFn({
          eventId: '8120',
          eventDesc: '绑定'
        })
        wx.showToast({
          title: '绑定成功',
        })
        wx.setStorageSync('mobile', this.data.form.phone)
        this.triggerEvent('success', { "mobile": this.data.form.phone})
        this.closeDialog();
      }).catch(e => {
        this.triggerEvent('error')
        app.mdFn({
          eventId: '8120',
          eventDesc: '绑定'
        })
        wx.showToast({
          icon: 'none',
          title: '绑定失败：' + e.faultstring,
        })
      })
    },
    // 获取验证码
    getYzm() {
      let phone = this.data.form.phone
      if (phone.length < 11) {
        wx.showToast({
          icon: 'none',
          title: '请输入正确的手机号码',
        })
        return
      }

      api.post('/api/login/sendverifcode', {
        data: {
          account: phone
        }
      }).then(res => {
        wx.showToast({
          title: '发送成功',
        })
        this.getYzmFn()
        console.log(res)

      }).catch(e => {
        console.log(e)
        wx.showToast({
          icon: 'none',
          title: '发送失败',
        })
      })
    },
    handlerPhoneNumber(res) {
      console.log(res)
      if (res.detail.errMsg === 'getPhoneNumber:ok') {
        // 获取成功，开始绑定
        app.isLogin({ ctx: this }).then(() => {
          this.saveWxMobile(res)
        }).catch(e => {
          this.saveWxMobile(res)
        })

      }
    },
    saveWxMobile(res) {
      api.post(`/api/user/savewamobile/${CONFIG.orgCode}/${CONFIG.appCode}`, {
        data: {
          encrypteddata: res.detail.encryptedData,
          iv: res.detail.iv,
        }
      }).then(res => {
        console.log(res)
        wx.showToast({
          title: '绑定成功',
        })
        app.isLogin({ ctx: this })
        this.triggerEvent('success',{ "mobile": this.data.form.phone })
        this.closeDialog();
      }).catch(e => {
        console.log(e)
        wx.showToast({
          icon: 'none',
          title: '绑定失败：' + e.faultstring,
        })
        this.triggerEvent('error')
      })
    },

  }
})
